package co.edu.unal.bioingenium.kbmed.knowledge.mapping.vo;

import java.util.Arrays;
import java.util.BitSet;

/**
 *
 * @author  Alejandro Riveros Cruz
 */
public class DescriptorScore implements Comparable<DescriptorScore> {

    private double totalScore;
    private short descriptorTokens;
    private double[] scores;
    private BitSet bitSet;
    private short[] positions;

    private DescriptorScore() {
    }

    /**
     * @param:
     * @return:
     */
    public DescriptorScore(int senteceLength) {
        totalScore = 0;
        scores = new double[senteceLength];
        positions = new short[senteceLength];
        bitSet = new BitSet(senteceLength);
        Arrays.fill(positions, (short) -1);
    }

    /**
     * @param:
     * @return:
     */
    public double getTotalScore() {
        return totalScore;
    }

    /**
     * @param:
     * @return:
     */
    public void setTotalScore(double score) {
        if (score == Double.NaN || score == Double.POSITIVE_INFINITY || score == Double.NEGATIVE_INFINITY) {
            throw new NumberFormatException(); //TODO afver validation this can be changed by set zero
        }
        this.totalScore = score;
    }

    /**
     * 
     * @return
     */
    public double[] getScores() {
        return scores;
    }

    /**
     * @param:
     * @return:
     */
    public short[] getPositions() {
        return positions;
    }

    /**
     * 
     * @param wordSentencePosition
     * @param wordConceptPosition
     * @param similarity
     */
    public void setScore(int wordSentencePosition, short wordConceptPosition, double similarity) {
        scores[wordSentencePosition] = similarity;
        positions[wordSentencePosition] = wordConceptPosition;
        bitSet.set(wordSentencePosition);
    }

    /**
     * 
     * @return
     */
    public int matches() {
        return bitSet.cardinality();
    }

    /**
     * 
     * @return
     */
    public short getDescriptorTokens() {
        return descriptorTokens;
    }

    /**
     * 
     * @param numTokens
     */
    public void setDescriptorTokens(short numTokens) {
        this.descriptorTokens = numTokens;
    }

    /**
     * 
     * @return
     */
    public BitSet getBitSet() {
        return bitSet;
    }

    @Override
    public int compareTo(DescriptorScore o) {
        if (this.totalScore > o.getTotalScore()) {
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("scores : ");
        for (int i = 0; i < scores.length; i++) {
            builder.append(scores[i]).append(" ");
        }
        builder.append("\t totalScore:").append(totalScore);
        return builder.toString();
    }
}
